import os
import string
import sys
import time

from xjlibrary.mrequest.baserequest import BaseRequest
from xjlibrary.our_file_dir.base_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(TopPath, "download", "jstor", "download", "home")
BaseDir.create_dir(sPath)

HEADERS = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
    "upgrade-insecure-requests": "1",
    "cache-control": "no-cache",
    "pragma": "no-cache",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 "
                  "Safari/537.36 "
}

BaseUrl = "https://www.jstor.org/action/showJournals?contentType=journals&letter="


def requestJournals(url):
    BoolResult, errString, r = BaseRequest(url, mark="visuallyhidden", headers=HEADERS, timeout=45)
    if not BoolResult:
        print("请检查失败原因:" + errString)
        sys.exit(-1)
    return r


def savefile(r, filePath):
    BaseDir.single_write_file(r.text, filePath)


def start():
    letter = "0-9"
    url = BaseUrl + letter
    filePath = os.path.join(sPath, letter + ".html")
    if not os.path.exists(sPath):
        os.makedirs(sPath)
    if os.path.exists(filePath):
        print("文件存在" + filePath)
    else:
        r = requestJournals(url)
        savefile(r, filePath)
    for letter in string.ascii_uppercase:
        filePath = os.path.join(sPath, letter + ".html")
        if os.path.exists(filePath):
            print("文件存在" + filePath)
            continue
        url = BaseUrl + letter
        r = requestJournals(url)
        savefile(r, filePath)
        time.sleep(3)

    print("请求完成")


def main():
    start()


if __name__ == "__main__":
    main()
